<template>
  <div class="horizontal-timeline-container">
    <div class="timeline-header">
      <h3>网站发展历程</h3>
      <p>记录网站从创建到现在的每一个重要时刻</p>
    </div>
    
    <div class="timeline-wrapper">
      <div class="horizontal-timeline">
        <!-- 时间线轴 -->
        <div class="timeline-axis" ref="timelineAxis" :style="{ width: timelineWidth }">
          <!-- 渐变色彩线条 -->
          <div class="timeline-line-gradient"></div>
          
          <!-- 时间点标记 -->
          <div 
            v-for="(event, index) in timelineEvents" 
            :key="index" 
            class="timeline-event-marker"
            :class="{ 'active': activeIndex === index }"
            :style="{ left: `${index * 120 + 60}px` }"
            @mouseenter="setActiveIndex(index)"
            @click="scrollToEvent(index)"
          >
            <div class="event-label">{{ event.date }}</div>
          </div>
        </div>
      </div>
    </div>
    
    <!-- 时间线内容卡片 -->
    <div class="timeline-content-card" v-if="timelineEvents.length > 0">
      <h4 class="timeline-title">{{ timelineEvents[activeIndex].title }}</h4>
      <p class="timeline-description">{{ timelineEvents[activeIndex].description }}</p>
    </div>
  </div>
</template>

<script>
export default {
  name: 'HorizontalTimeline',
  data() {
    return {
      activeIndex: 0,
      timelineEvents: [
        {
          date: "2023-05-21",
          title: "注册域名",
          description: "注册了域名，标志着网站项目的正式启动。"
        },
        {
          date: "2023-06-09",
          title: "网站初版上线",
          description: "网站初版v1.0页面正式上线，实现了基础的展示功能。"
        },
        {
          date: "2023-09-19",
          title: "WebGL背景",
          description: "网站开始使用2D WebGL技术渲染背景，提升了视觉效果。"
        },
        {
          date: "2023-10-08",
          title: "新增音乐模块",
          description: "网站新增音乐模块，为用户提供了更好的听觉体验。"
        },
        {
          date: "2023-10-19",
          title: "新增留言板",
          description: "网站新增留言板模块，增强了用户互动功能。"
        },
        {
          date: "2023-11-07",
          title: "ICP备案",
          description: "网站完成ICP备案，获得了在中国大陆合法运营的资格。"
        },
        {
          date: "2023-11-28",
          title: "全新UI界面",
          description: "网站使用全新UI界面2.0，提升了用户体验和视觉效果。"
        },
        {
          date: "2024-10-18",
          title: "新增旅游足迹",
          description: "网站新增个人旅游足迹模块，展示个人旅行经历。"
        },
        {
          date: "2025-03-05",
          title: "新增绿洲模块",
          description: "网站新增绿洲模块，丰富了网站内容和功能。"
        },
        {
          date: "2025-05-20",
          title: "新增弹幕功能",
          description: "网站新增弹幕功能，增强了用户互动体验。"
        },
        {
          date: "2025-06-12",
          title: "重写技术栈关系图",
          description: "重写了技术栈关系图，更好地展示了项目的技术架构。"
        },
        {
          date: "2025",
          title: "项目施工中",
          description: "项目仍在持续开发中，未来将会有更多功能和改进。"
        }
      ]
    };
  },
  computed: {
    timelineWidth() {
      // 根据事件数量计算时间轴宽度，每个事件占120px，加上左右边距
      return `${this.timelineEvents.length * 120 + 100}px`;
    }
  },
  methods: {
    setActiveIndex(index) {
      this.activeIndex = index;
    },
    // 滚动到指定时间点
    scrollToEvent(index) {
      this.setActiveIndex(index);
      const marker = this.$refs.timelineAxis.children[index + 1]; // +1 because of the gradient line
      if (marker) {
        const container = this.$refs.timelineAxis.parentElement.parentElement;
        const containerWidth = container.offsetWidth;
        const markerPosition = marker.offsetLeft;
        const scrollPosition = markerPosition - containerWidth / 2;
        container.scrollTo({
          left: scrollPosition,
          behavior: 'smooth'
        });
      }
    }
  },
  mounted() {
    // 添加鼠标滚轮事件监听器，用于水平滚动
    const timelineWrapper = this.$el.querySelector('.timeline-wrapper');
    if (timelineWrapper) {
      timelineWrapper.addEventListener('wheel', (e) => {
        if (e.deltaY !== 0) {
          e.preventDefault();
          timelineWrapper.scrollLeft += e.deltaY;
        }
      });
    }
  }
};
</script>

<style scoped>
.horizontal-timeline-container {
  background: #333;
  border-radius: 8px;
  padding: 20px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.3);
  font-family: 'Arial', sans-serif;
  max-width: 1000px;
  margin: 0 auto;
}

.timeline-content-card {
  background: #2d2d2d;
  border-radius: 8px;
  padding: 20px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
  min-height: 180px;
  transition: all 0.3s ease;
  margin-top: 20px;
}

.timeline-header {
  text-align: center;
  margin-bottom: 30px;
}

.timeline-header h3 {
  color: #fff;
  font-size: 24px;
  margin-bottom: 10px;
}

.timeline-header p {
  color: #ccc;
  font-size: 14px;
}

.timeline-wrapper {
  overflow-x: auto;
  padding: 10px 0;
}

/* 自定义滚动条样式 */
.timeline-wrapper::-webkit-scrollbar {
  height: 8px;
}

.timeline-wrapper::-webkit-scrollbar-track {
  background: #2d2d2d;
  border-radius: 4px;
}

.timeline-wrapper::-webkit-scrollbar-thumb {
  background: #409eff;
  border-radius: 4px;
}

.timeline-wrapper::-webkit-scrollbar-thumb:hover {
  background: #66b1ff;
}

.horizontal-timeline {
  position: relative;
  width: 100%;
}

/* 时间线轴 */
.timeline-axis {
  position: relative;
  height: 80px;
  margin-bottom: 40px;
  min-width: 100%;
  padding: 0 20px;
}

/* 渐变色彩线条 */
.timeline-line-gradient {
  position: absolute;
  top: 50%;
  left: 0;
  right: 0;
  height: 6px;
  transform: translateY(-50%);
  background: linear-gradient(90deg, #409eff, #67c23a, #e6a23c, #f56c6c, #909399);
  border-radius: 3px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
  z-index: 1;
}

/* 时间点标记 */
.timeline-event-marker {
  position: absolute;
  top: 50%;
  transform: translate(-50%, -50%);
  width: 16px;
  height: 16px;
  background: #fff;
  border-radius: 50%;
  cursor: pointer;
  z-index: 2;
  transition: all 0.3s ease;
  box-shadow: 0 0 0 2px #333, 0 0 0 4px #409eff;
}

.timeline-event-marker:hover,
.timeline-event-marker.active {
  transform: translate(-50%, -50%) scale(1.3);
  box-shadow: 0 0 0 2px #333, 0 0 0 6px rgba(64, 158, 255, 0.5);
}

/* 时间点标签 */
.event-label {
  position: absolute;
  top: 25px;
  left: 50%;
  transform: translateX(-50%);
  color: #ccc;
  font-size: 12px;
  white-space: nowrap;
  transition: all 0.3s ease;
}

.timeline-event-marker:hover .event-label,
.timeline-event-marker.active .event-label {
  color: #409eff;
  font-weight: bold;
}

.timeline-title {
  color: #fff;
  font-size: 20px;
  margin: 0 0 15px 0;
}

.timeline-description {
  color: #ccc;
  font-size: 14px;
  line-height: 1.6;
  margin: 0 0 15px 0;
}

.timeline-event-details {
  margin-top: 15px;
}

.event-date {
  color: #409eff;
  font-weight: bold;
  margin-bottom: 10px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .horizontal-timeline-container {
    padding: 15px;
  }
  
  .timeline-header h3 {
    font-size: 20px;
  }
  
  .timeline-axis {
    height: 70px;
  }
  
  .event-label {
    font-size: 10px;
    top: 20px;
  }
  
  .timeline-content-card {
    padding: 15px;
    min-height: 160px;
  }
  
  .timeline-title {
    font-size: 18px;
  }
  
  .timeline-description {
    font-size: 13px;
  }
  
  .event-date {
    font-size: 12px;
  }
}

@media (max-width: 480px) {
  .timeline-axis {
    height: 60px;
  }
  
  .timeline-event-marker {
    width: 14px;
    height: 14px;
  }
  
  .event-label {
    font-size: 9px;
    top: 18px;
  }
  
  .timeline-content-card {
    padding: 12px;
    min-height: 140px;
  }
  
  .timeline-title {
    font-size: 16px;
    margin-bottom: 10px;
  }
  
  .timeline-description {
    font-size: 12px;
    margin-bottom: 10px;
  }
}

/* 大屏幕优化 */
@media (min-width: 1200px) {
  .timeline-axis {
    min-width: 1000px;
  }
}
</style>